{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting a single ping example\n",
    "This script demonstrates ploting a single ping as power for every channel in a\n",
    "specified raw file.\n",
    "\n",
    "NOTE: This example was written before the get_power method was added to the ProcessedData object, where you just call EK60.raw_data.get_power() to get the power data.  So instead, this example reaches into the EK60RawData objects directly to pull out power.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.pyplot import cm\n",
    "from echolab2.instruments import EK60"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read in the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the path to the data file.\n",
    "raw_filename = \"./data/EK60/DY1706_EK60-D20170609-T005736.raw\"\n",
    "\n",
    "# Create an instance of our EK60 object.\n",
    "ek60 = EK60.EK60()\n",
    "\n",
    "# Read in the .raw data file.\n",
    "print('Reading raw file %s' % (raw_filename))\n",
    "ek60.read_raw(raw_filename)\n",
    "\n",
    "# Print some info about the state of our EK60 object.\n",
    "print(ek60)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parse and plot the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set up the colormap for plotting.\n",
    "color = iter(cm.rainbow(np.linspace(0, 1, len(ek60.channel_ids))))\n",
    "\n",
    "# Create a matplotlib figure to plot our echograms on.\n",
    "fig = plt.figure(figsize=(7, 7))\n",
    "\n",
    "# Get references.\n",
    "\n",
    "# Specify the ping number to plot.\n",
    "ping_number = 100\n",
    "\n",
    "# Plot power from the specified ping from all channels.\n",
    "for channel_id in ek60.channel_ids:\n",
    "\n",
    "    # Get a reference to the RawData object for this channel.\n",
    "    raw_data = ek60.get_raw_data(channel_id=channel_id)\n",
    "\n",
    "    # Get a color for this channel.\n",
    "    c = next(color)\n",
    "\n",
    "    # Create a sample index so we can pass it as the Y axis in the figure so\n",
    "    # we plot the data up and down.\n",
    "    n_samples = len(raw_data.power[ping_number, :])\n",
    "    yaxis = np.arange(n_samples)\n",
    "\n",
    "    # Plot power.\n",
    "    plt.plot(raw_data.power[ping_number, :], yaxis, color=c, label=channel_id)\n",
    "\n",
    "# Label the figure and set other display properties.\n",
    "plt.gca().invert_yaxis()\n",
    "plt.ylabel('Sample')\n",
    "plt.xlabel('Power')\n",
    "title = 'Ping %i' % (ping_number)\n",
    "fig.suptitle(title, fontsize=14)\n",
    "plt.legend()\n",
    "\n",
    "# Display plot.\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
